const api = require('../../../../ws_micro_shop/utils/api.js');
const tools = require('../../../../ws_micro_shop/utils/getuserinfo.js');
const app = getApp();
const { module: moduleName } = app.siteInfo;
const util = app.util;

let pageNum = 2;

Page({
  data: {
    cid: 0,
    scrollLeft: 600,
    scrollTop: 0,
    emptyGoods: 0,
    page_count: 0,
    cat_show: 1,
    classa: (app.tabbarindex === 0 ? false : true) || app.siteInfo.tabbar_type === '2',
    theme: app.themeData,
    abnorData: {
      imgSrc: '/common/static/images/abnor-08.png',
      text: '暂无相关商品',
    },
    unit: '',
    showsearch: false,
    keywords: '',
    useTabbar: !(app.tabbarindex === 0 && app.siteInfo.tabbar_type === '1'),
  },
  onLoad(opts) {
    let options = opts || {};
    if (options && options.shopid) {
      app.ws_micro_shop_globalData.shopid = options.shopid;
    }
    app.tabbarindex = options.tabbarindex ? options.tabbarindex - 0 : app.tabbarindex;
    app.pageOnLoad(this);
    let that = this;
    let userinfo = wx.getStorageSync('user_info');
    app.loadCurrencyUnit().then(res => {
      that.setData({
        unit: res.unit,
      });
    });
    if (userinfo) {
      Number(userinfo.info === 0) && tools.onGetInfo(this, userinfo.auth_mode);
      util.prepareBindInfo(options, moduleName, that);
    } else {
      app.ws_micro_shoplogin().then(res => {
        userinfo = wx.getStorageSync('user_info');
        Number(userinfo.info === 0) && tools.onGetInfo(that, userinfo.auth_mode);
        util.prepareBindInfo(options, moduleName, that);
      });
    }
    let cid = options.cid;
    cid && this.loadGoodsList(cid);
    this.setData({
      opts: options,
      cid: cid || '',
      showtoindex: app.isSinglePage(this.route, getCurrentPages().length),
    });
    this.systemInfo = wx.systemInfo;
    this.loadIndexInfo(options);
  },
  onShareAppMessage() {
    let path = '/ws_micro_shop_book/pages/book/index/index?tabbarindex=' + app.tabbarindex + '&shopid=' + app.ws_micro_shop_globalData.shopid;
    path = util.spliceSharePath(path, '4', moduleName);
    return {
      path,
    };
  },
  onShareTimeline() {
    return {
      title: "",
      imageUrl: "",
      query:
        "tabbarindex=" +
        app.tabbarindex +
        "&shopid=" +
        app.ws_micro_shop_globalData.shopid,
    };
  },
  onHide() {
    util.sendBindInfo(this.data);
  },
  onUnload() {
    util.sendBindInfo(this.data);
    pageNum = 2;
  },
  loadIndexInfo(opts) {
    let _this = this;
    wx.showLoading({
      title: '正在加载',
      mask: true,
    });
    app.ws_micro_shoprequest({
      url: api.book.index,
      method: 'GET',
      success: function (res) {
        if (res.code === 0) {
          setTimeout(function () {
            wx.hideLoading();
          }, 1000);
          let { cat, banner, ad, cat_show, goods } = res.data;
          _this.setData({
            cat,
            banner: banner || '',
            ad: ad || '',
            cat_show,
            page_count: goods.page_count,
          });
          if (!opts.cid) {
            _this.setData({
              goods: goods.list,
            });
          }
          if (goods.page >= goods.page_count) {
            _this.setData({
              emptyGoods: 1,
            });
          }
        }
      },
    });
  },
  switchNav(e) {
    let id = e.currentTarget.dataset.id;
    wx.showLoading({
      title: '正在加载',
      mask: true,
    });
    let cid = 0;
    pageNum = 2;
    if (Number(cid) === Number(id) && Number(id) !== 0) { return }
    cid = id;
    let windowWidth = this.systemInfo.windowWidth;
    let offsetLeft = e.currentTarget.offsetLeft;
    let scrollLeft = this.data.scrollLeft;
    (offsetLeft > windowWidth / 2) ? scrollLeft = offsetLeft : scrollLeft = 0;
    this.setData({
      cid,
      page: 1,
      scrollLeft,
      scrollTop: 0,
      emptyGoods: 0,
      goods: [],
      show_loading_bar: 1,
    });
    this.loadGoodsList(cid);
  },
  loadGoodsList(cid) {
    let _this = this;
    let keywords = _this.data.keywords;
    app.ws_micro_shoprequest({
      url: api.book.list,
      method: 'GET',
      data: {
        cid,
        keywords,
      },
      success: function (res) {
        if (res.code === 0) {
          setTimeout(function () {
            wx.hideLoading();
          }, 1000);
          let { page_count, page, list: goods, row_count } = res.data;
          if (page_count >= page) {
            _this.setData({
              goods,
              page_count,
              row_count,
              show_loading_bar: 0,
            });
          } else {
            _this.setData({
              emptyGoods: 1,
            });
          }
        }
      },
    });
  },
  onReachBottom(e) {
    let _this = this;
    let { emptyGoods, show_loading_bar, cid, keywords } = _this.data;
    if (Number(emptyGoods) === 1 || Number(show_loading_bar) === 1) { return }
    _this.setData({
      show_loading_bar: 1,
    });
    app.ws_micro_shoprequest({
      url: api.book.list,
      method: 'GET',
      data: {
        page: pageNum,
        cid,
        keywords,
      },
      success: function (res) {
        if (res.code === 0) {
          let { page, list, page_count, row_count } = res.data;
          let goods = _this.data.goods;
          if (page >= pageNum) {
            Array.prototype.push.apply(goods, list);
          }
          if (page_count >= pageNum) {
            _this.setData({
              goods,
              page,
              page_count,
              row_count,
              show_loading_bar: 0,
            });
          } else {
            _this.setData({
              emptyGoods: 1,
            });
          }
          pageNum++;
        }
      },
    });
  },
  showSearchBox() {
    this.setData({
      showsearch: !this.data.showsearch,
      keywords: '',
    });
  },
  getSearchTxt(e) {
    this.setData({
      keywords: (e.detail.value).trim(),
    });
  },
  sureSearch() {
    this.setData({
      goods: [],
    });
    this.loadGoodsList(this.data.cid);
  },
  collectFormId(e) {
    app.collectFormId(e);
  },
});
